package com.example.service.flowable.adm.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mapper.*;
import com.example.pojo.*;
import com.example.service.flowable.adm.IGsPyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 物品申请表 服务实现类
 * </p>
 * @author wsl
 * @since 2022-04-11
 *
 */
@Service
public class GsPyServiceImpl extends ServiceImpl<GsPyMapper, GsPy> implements IGsPyService {

    @Resource
    private GsPyMapper gsPyMapper;

    @Autowired
    private GsPyDetailMapper gsPyDetailMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private GsTypeMapper gsTypeMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional
    public ResultVo createGsPy(GsPy gsPy) {
        int insert = baseMapper.insert(gsPy);
        gsPy.getGsPyDetailList().forEach(e->{
            e.setGoodsPyId(gsPy.getId());
            gsPyDetailMapper.insert(e);
        });
        return ResultVo.ok();
    }

    @Override
    public PageInfo<GsPy> getAllGsPy(Integer empId,Integer deptId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<GsPy> wrapper= new QueryWrapper<>();
        wrapper.eq(empId!=null,"emp_id",empId);
        wrapper.eq(deptId!=null,"dept_id",deptId);

        List<GsPy> list = baseMapper.selectList(wrapper.orderByDesc("id"));
        list.forEach(e->{
            List<GsPyDetail> list1 = gsPyDetailMapper.selectList(new QueryWrapper<GsPyDetail>().eq("goods_py_id", e.getId()));
//            list1.forEach(e1->{
//                gsPyDetailMapper.selectById(e.getId());
//            });
            Emp emp = empMapper.selectById(e.getEmpId());
            Dept dept = deptMapper.selectById(e.getDeptId());

            e.setEmp(emp);
            e.setDept(dept);
            e.setGsPyDetailList(list1);
        });
        return new PageInfo<>(list);
    }

//    @Override
//    @Transactional
//    public ResultVo addGsPy(GsPy gsPy) {
//        //1、新建物品申请记录
//        String processCode = ProKeyGenerator.generate("WPSQ");
//        gsPy.setProcessCode(processCode);
//        boolean save = save(gsPy);
//        //2、添加上述物品申请详情
//        for (GsPyDetail gsPyDetail : gsPy.getGsPyDetailList()) {
//            gsPyDetail.setGoodsPyId(gsPy.getId());
//            gsPyDetailService.save(gsPyDetail);
//        }
//        //3、加入流程管理，新增流程记录
//        ProcessRecord processRecord = new ProcessRecord();
//        Integer operatorId = gsPy.getEmpId();
//        processRecord
//                .setProcessCode(processCode)
//                .setOperatorId(operatorId);
//        processRecordService.save(processRecord);
//        return save ? ResultVo.success("添加成功") : ResultVo.error("添加失败!");
//    }
    //查询当前登录者的资产
    public ResultVo selectMyGsPt(Integer userId,String name) {
        List<GsPy> list= baseMapper.selectMyGsPt(userId,name);
        return ResultVo.response(list);
    }

    //同意申请修改物品总数量
    @Override
    public ResultVo auditTask(Integer gsId, Boolean approved) {
        //1、根据gsId修改申请表状态
//        List<GsPy> list =baseMapper.updateStatus(gsId,approved);
//        list.forEach(e->{
//            List<GsPyDetail> list1= gsPyDetailMapper.selectList(new QueryWrapper<GsPyDetail>().eq("id",e.getId()));
//            e.setGsPyDetailList(list1);
//        });



//        if(approved){
//
//            List<GsPy> list= baseMapper.selectList(null);
//            list.forEach(e->{
//                GsPy gsPy = new GsPy().setId(gsId).setStatus(1);
//                gsPyMapper.update(gsPy,new UpdateWrapper<GsPy>().eq("id",gsPy.getId()));
//            });
//
//
//            //2、根据gsId查询详情表记录，遍历~
//
//
//
//            GsPyDetail gsPyDetail = new GsPyDetail();
//            gsPyDetail.getGsId();//物品编号
//            gsPyDetail.getNum();//借的数量
//
//
//
//
//
//        }else{
//
//        }
//        System.out.println(list);
        return null;
    }
//    @Override
//    public ResultVo queryGsPyList() {
//        return null;
//    }
}
